package com.guosong.rabbitmq.pubsub;

import com.guosong.rabbitmq.utils.RabbitConstant;
import com.guosong.rabbitmq.utils.RabbitUtils;
import com.rabbitmq.client.*;

import java.io.IOException;

public class BaiDu {
    public static void main(String[] args) throws IOException {
        //获取TCP常连接
        Connection connection = RabbitUtils.getConnection();
        //获取虚拟连接
        final Channel channel = connection.createChannel();
        //声明队列信息
        channel.queueDeclare(RabbitConstant.QUEUE_BAIDU,false,false,false,null);
        //queueBind用于将队列与交换机绑定
        //参数1：队列名 参数2：交换机名称，参数三 路由key(暂时用不到）
        channel.queueBind(RabbitConstant.QUEUE_BAIDU,RabbitConstant.EXCHANGE_WATCHER,"");
        channel.basicQos(1);
        channel.basicConsume(RabbitConstant.QUEUE_BAIDU,false,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                System.out.println("百度收到消息"+new String(body));
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        });




    }
}
